package com.example.jixeishi1.controller;


import com.example.jixeishi1.entity.AddressEntity;
import com.example.jixeishi1.service.AddressService;
import com.example.jixeishi1.utils.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AddressController {
    @Resource
    AddressService addressService;

    @RequestMapping("address/list")
    public R list(){
        List<AddressEntity> addressEntities = new ArrayList<>();
        addressEntities = addressService.getListAddressByUid();
        ModelMap map = new ModelMap();
        map.put("address",addressEntities);
        return R.ok().put("address",addressEntities);
    }


    @PostMapping("address/save")
    public ModelAndView saveAdddress( AddressEntity addressEntity){
        addressService.saveAddress(addressEntity);
        ModelAndView mav = new ModelAndView("redirect:/payment");
        return mav;
    }

    @RequestMapping(value = "/address/delete/{id}")
    public ModelAndView deleteAllAddress(@PathVariable("id") String id){
        Integer ids =Integer.parseInt(id);
        addressService.delete(ids);
        ModelAndView mav = new ModelAndView("redirect:/payment");
        return mav;
    }

}
